/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.product.web.attribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.product.entity.attribute.TbProductAttribute;
import com.thinkgem.jeesite.modules.product.service.attribute.TbProductAttributeService;

/**
 * 产品属性Controller
 * @author 宋子军
 * @version 2016-11-22
 */
@Controller
@RequestMapping(value = "${adminPath}/product/attribute/tbProductAttribute")
public class TbProductAttributeController extends BaseController {

	@Autowired
	private TbProductAttributeService tbProductAttributeService;
	
	@ModelAttribute
	public TbProductAttribute get(@RequestParam(required=false) String id) {
		TbProductAttribute entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tbProductAttributeService.get(id);
		}
		if (entity == null){
			entity = new TbProductAttribute();
		}
		return entity;
	}
	
	@RequiresPermissions("product:attribute:tbProductAttribute:view")
	@RequestMapping(value = {"list", ""})
	public String list(TbProductAttribute tbProductAttribute, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TbProductAttribute> page = tbProductAttributeService.findPage(new Page<TbProductAttribute>(request, response), tbProductAttribute); 
		model.addAttribute("page", page);
		return "modules/product/attribute/tbProductAttributeList";
	}

	@RequiresPermissions("product:attribute:tbProductAttribute:view")
	@RequestMapping(value = "form")
	public String form(TbProductAttribute tbProductAttribute, Model model) {
		model.addAttribute("tbProductAttribute", tbProductAttribute);
		return "modules/product/attribute/tbProductAttributeForm";
	}

	@RequiresPermissions("product:attribute:tbProductAttribute:edit")
	@RequestMapping(value = "save")
	public String save(TbProductAttribute tbProductAttribute, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tbProductAttribute)){
			return form(tbProductAttribute, model);
		}
		tbProductAttributeService.save(tbProductAttribute);
		addMessage(redirectAttributes, "保存产品属性成功");
		return "redirect:"+Global.getAdminPath()+"/product/attribute/tbProductAttribute/?repage";
	}
	
	@RequiresPermissions("product:attribute:tbProductAttribute:edit")
	@RequestMapping(value = "delete")
	public String delete(TbProductAttribute tbProductAttribute, RedirectAttributes redirectAttributes) {
		tbProductAttributeService.delete(tbProductAttribute);
		addMessage(redirectAttributes, "删除产品属性成功");
		return "redirect:"+Global.getAdminPath()+"/product/attribute/tbProductAttribute/?repage";
	}

}